package com.vivianwei.sqlfix;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author wangwei
 * @Date2021/3/10 2:58 下午
 * @Version V1.0
 **/
public class CommentProcess extends SqlProcess {


    public CommentProcess(List<String> tables, String excelPath, String genPath, String sheetName) {
        super(tables, excelPath, genPath, sheetName);
    }

    /**
     * select  COLUMN_TYPE from information_schema.`COLUMNS`
     * where table_schema = 'apollo' and table_name = 'suims_page' and column_name='PAGE_URI'
     *
     * @throws SQLException
     */
    @Override
    protected String sql(Map line) throws SQLException {
        List<Entity> list = Db.use((String) line.get("数据库名称")).query("select  COLUMN_TYPE from information_schema.`COLUMNS` \n" +
                        "where table_schema = ? and table_name = ? and column_name=?",
                line.get("数据库名称"), line.get("表名"), line.get("字段"));
        String columnType = (String) list.get(0).get("column_type");
//                alter table suims_page modify column PAGE_URI varchar(128) comment '页面url';
        return String.format("use %s;alter table %s modify column %s %s comment 'xxx';\n",
                line.get("数据库名称"), line.get("表名"), line.get("字段"), columnType);
    }
}
